///This page is used to Add/Edit a link to us entry.
///AUTHOR: KAJAL MANDAL
///DATE : 13 APRIL 2007
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Text;

public partial class Admin_Ad_AddEditResources : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlStatus.Items.Clear();
            ddlStatus.Items.Add(new ListItem("Approved", "A"));
            ddlStatus.Items.Add(new ListItem("Rejected", "R"));
            ddlStatus.Items.Add(new ListItem("Review", "V"));


            rowStatus.Visible = false;

            bool bInvalid = false;
            if (Request.QueryString.HasKeys())
            {
                if (Request.QueryString["cmd"] != null)
                {
                    if (Request.QueryString["cmd"].ToUpper() == "A")
                    {
                        lblHead.Text = "Add a Link";
                    }
                    else if (Request.QueryString["cmd"].ToUpper() == "E")
                    {
                        lblHead.Text = "Edit Link";
                        if (Session["link_id"] != null)
                        {
                            Manager mngr = new Manager(true,"ConnectionString");
                            SqlDataReader rd = mngr.ExecuteQuery("select sno,URL,Title,Description,Reciprocal_URL,SenderName,EMail,status,Date from tbl_linktous where isDelete=0 and sno=" + Session["link_id"].ToString());
                            if (rd.Read())
                            {
                                ViewState["sno"] = rd["sno"].ToString();
                                txtURL.Text = rd["URL"].ToString();
                                txtTITLE.Text = rd["Title"].ToString();
                                txtDESCRIPTION.Text = rd["Description"].ToString();
                                txtRECIPROCAL.Text = rd["Reciprocal_URL"].ToString();
                                txtSENDERNAME.Text = rd["SenderName"].ToString();
                                txtEMAIL.Text = rd["EMail"].ToString();
                                rowStatus.Visible = true;
                                switch (rd["status"].ToString().ToUpper())
                                {
                                    case "A":
                                        ddlStatus.SelectedIndex = 0;
                                        break;
                                    case "R":
                                        ddlStatus.SelectedIndex = 1;
                                        break;
                                    case "V":
                                        ddlStatus.SelectedIndex = 2;
                                        break;
                                }
                            }
                            else
                            {
                                lblMsg.ForeColor = System.Drawing.Color.Red;
                                lblMsg.Text = "Record not found!";
                            }
                            rd.Close();
                            //Session.Remove("link_id");
                        }
                        else
                        {
                            bInvalid = true;
                        }
                    }
                    else
                    {
                        bInvalid = true;
                    }
                }
                else
                {
                    bInvalid = true;
                }
            }
            else
            {
                bInvalid = false;
            }

            if (bInvalid) Response.Redirect(Session["Strhttp"].ToString() + "admin/Ad_logout.aspx", true);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlParameter[] prm = new SqlParameter[10];

        prm[0] = new SqlParameter("@sno", SqlDbType.BigInt);
        prm[1] = new SqlParameter("@URL", SqlDbType.VarChar, 100);
        prm[2] = new SqlParameter("@Title", SqlDbType.VarChar, 200);
        prm[3] = new SqlParameter("@Description", SqlDbType.VarChar, 500);
        prm[4] = new SqlParameter("@Reciprocal_URL", SqlDbType.VarChar, 100);
        prm[5] = new SqlParameter("@Sender", SqlDbType.VarChar, 100);
        prm[6] = new SqlParameter("@EMail", SqlDbType.VarChar, 100);
        prm[7] = new SqlParameter("@isDelete", SqlDbType.Bit);
        prm[8] = new SqlParameter("@status", SqlDbType.Char, 1);
        prm[9] = new SqlParameter("@SUCCESS", SqlDbType.Bit);
        
        prm[1].Value = txtURL.Text;
        prm[2].Value = txtTITLE.Text;
        prm[3].Value = txtDESCRIPTION.Text;
        prm[4].Value = txtRECIPROCAL.Text;
        prm[5].Value = txtSENDERNAME.Text;
        prm[6].Value = txtEMAIL.Text;
        bool sMail = true;
        if (ViewState["sno"] != null)
        {
            prm[0].Value = Convert.ToInt64(ViewState["sno"]);

            if (ddlStatus.SelectedValue == "R")
            { prm[7].Value = 0; }
            else
            { prm[7].Value = 0; }
            
            prm[8].Value = ddlStatus.SelectedItem.Value;
        }
        else
        {
            prm[0].Value = 0;
            prm[7].Value = 0;
            prm[8].Value = "A";
            sMail = false ;
        }

        prm[9].Direction = ParameterDirection.Output;

        Manager mngr = new Manager(true,"ConnectionString");
        mngr.ExecuteUpdate("MANAGE_LINK_TO_US", prm);

        if (Convert.ToBoolean(prm[9].Value))
        {
            lblMsg.ForeColor = System.Drawing.Color.DarkGreen;
            if (rowStatus.Visible)
            {
                int ret=0;
                if (prm[8].Value.ToString().ToUpper() == "A" && sMail )
                { if (Send_Mail_2()) ret = 1; else ret = 2; }
                else if (prm[8].Value.ToString().ToUpper() == "R" && sMail)
                { if (Send_Mail_3()) ret = 1; else ret = 2; }

                lblMsg.Text = "Resource has been successfully updated.";

                if (ret > 0)
                {
                    if (ret == 1)
                    {
                        lblMsg.Text += "<br>Confirmtation Mail sent to link sender!";
                    }
                    else
                    {
                        lblMsg.Text += "<br>Confirmtation Mail sending FAILED!";
                    }
                }
            }
            else
            { lblMsg.Text = "Resource has been successfully added."; }
        }
        else
        {
            lblMsg.ForeColor = System.Drawing.Color.Red;
            lblMsg.Text = "Sorry! Due to some technical problem, we are unable to receive this post!";
        }

        mngr = null;

        return;
    }


    protected bool Send_Mail_2()
    {
        StringBuilder strBody = new StringBuilder(string.Empty);
        Manager mngr = new Manager(true,"ConnectionString");

        strBody.Append("Hi ");
        strBody.Append(txtSENDERNAME.Text);
        strBody.Append(",<p>Congratulations!</p>");
        strBody.Append("<p>Your Link request has been accepted into <a href=\"www.findadoc.com\">www.findadoc.com</a>.</p>");
        strBody.Append("<p>Please review the link on our website.<br></p>");
        strBody.Append("<p>Thank You!<br>FindaDoc Team");
        bool ret = false;
        try
        {
            mngr.sendMail(strBody.ToString(), txtEMAIL.Text, "FindADoc Team", "Link Submission Accepted", "info@findadoc.com", "");
            ret = true;
        }
        catch (Exception excp)
        {
            mngr.sendMail(excp.Message, "synapsetesting@gmail.com", "FindADoc Team", "ERROR - Link Submission", "info@findadoc.com", "");
        }

        mngr = null;
        return ret;
    }

    protected bool Send_Mail_3()
    {
        StringBuilder strBody = new StringBuilder(string.Empty);
        Manager mngr = new Manager(true,"ConnectionString");

        strBody.Append("Hi ");
        strBody.Append(txtSENDERNAME.Text);
        strBody.Append("<p>Your Link request has been rejected into <a href=\"www.findadoc.com\">www.findadoc.com</a> as the website is not fulfilling our criteria for Link Exchange.</p>");
        strBody.Append("<p><br>Thank You!<br>FindaDoc Team");
        bool ret = false;
        try
        {
            mngr.sendMail(strBody.ToString(), txtEMAIL.Text, "FindADoc Team", "Link Submission Rejected", "info@findadoc.com", "");
            ret = true;
        }
        catch (Exception excp)
        {
            mngr.sendMail(excp.Message, "synapsetesting@gmail.com", "FindADoc Team", "ERROR - Link Submission", "info@findadoc.com", "");
        }

        mngr = null;
        return ret;
    }
}
